package com.xcity.pomelo.response;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.xcity.db.entity.Player;
import com.xcity.game.financial.FinancialService;
import com.xcity.game.financial.FinancialTemplate;
import com.xcity.game.financial.PlayerFinancial;

import gnu.trove.map.TIntObjectMap;
import gnu.trove.map.hash.TIntObjectHashMap;
import naga.x.App;
import naga.x.game.template.TemplateService;
import naga.x.net.impl.pomelo.PomeloMessage;
import naga.x.net.impl.pomelo.protocol.Message;

@PomeloMessage(type = Message.Type.RESPONSE)
public class MyFinancialsResponse extends Response {

	private List<Financial> list;
	
	public static class Financial {
		private int id; // 理财产品id
		private String name; // 理财产品名
		private long availableInterest; // 已领取的利息
		private long interest; // 总利息
		private long principal; // 本金
		private int rate; // 收益率
		private byte state; // 状态(0:不能领取;1:可领取)
		public Financial(int id, String name, long availableInterest, long interest, long principal, int rate, byte state) {
			this.id = id;
			this.name = name;
			this.availableInterest = availableInterest;
			this.interest = interest;
			this.principal = principal;
			this.rate = rate;
			this.state = state;
		}
		public int getId() {
			return id;
		}
		public void setId(int id) {
			this.id = id;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public long getAvailableInterest() {
			return availableInterest;
		}
		public void setAvailableInterest(long availableInterest) {
			this.availableInterest = availableInterest;
		}
		public long getInterest() {
			return interest;
		}
		public void setInterest(long interest) {
			this.interest = interest;
		}
		public long getPrincipal() {
			return principal;
		}
		public void setPrincipal(long principal) {
			this.principal = principal;
		}
		public int getRate() {
			return rate;
		}
		public void setRate(int rate) {
			this.rate = rate;
		}
		public byte getState() {
			return state;
		}
		public void setState(byte state) {
			this.state = state;
		}
	}
	
	public MyFinancialsResponse(int serial, Player player) {
		super(serial);
		List<PlayerFinancial> srcs = player.getFinancials().getFinancials();
		if (srcs == null || srcs.isEmpty()) {
			list = Collections.emptyList();
			return;
		}
		TIntObjectMap<Financial> map = new TIntObjectHashMap<Financial>();
		for (int i = 0, size = srcs.size(); i < size; i++) {
			PlayerFinancial src = srcs.get(i);
			Financial f = map.get(src.getId());
			if (f == null) {
				FinancialTemplate temp = App.getApp().getServiceManager().get(TemplateService.class).get(FinancialTemplate.class, src.getId());
				if (temp == null) {
					temp = FinancialService.defaultTemplate;
				}
				f = new Financial(src.getId(), temp.getName(), src.getInterestGot(), src.getInterest(), src.getAmount(), temp.getRate(), (byte) 0);
				map.put(src.getId(), f);
			} else {
				f.availableInterest += src.getInterestGot();
				f.interest += src.getInterest();
				f.principal += src.getAmount();
			}
			if (src.canReceiveInterest()) { // 只要有一个产品能领取利息就设置为1
				f.state = 1;
			}
		}
		list = new ArrayList<Financial>(map.valueCollection());
	}
	
	public List<Financial> getList() {
		return list;
	}

	public void setList(List<Financial> list) {
		this.list = list;
	}


}
